<?php
/**
 * | AndPHP框架[基于ThinkPHP5开发]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2017-2019 http://www.andphp.com
 * +----------------------------------------------------------------------
 * | AndPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
 * +----------------------------------------------------------------------
 * | author    :BabySeeME <417170808@qq.com>
 * +----------------------------------------------------------------------
 * | createTime :2018/3/9 000913:36
 * +----------------------------------------------------------------------
 */

namespace app\user\controller;


use app\common\controller\UserController;
use app\common\model\User as UserModel;
use think\facade\Session;

class Login extends UserController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index(){
        if(Session::has('status')){
            $this->redirect('/user/index');
        }
        return $this->fetch();
    }
    public function into(){

        if (request()->isPost()) {
            $member = new UserModel();
            $data = input('post.');

            $userStr = $data['username'];
            //用户名、邮箱、手机号均可登陆
            if (preg_match("/^1[34578]\d{9}$/", $userStr)) {
                $map['phone'] = $userStr;
            } else {
                $map['username|email'] = $userStr;
            }

            $user = $member->where('status','gt',0)->where($map)->where('is_delete',0)->find();

            if ($user) {
                if ($user['password'] == passwordMD5($data['password'],$user['salt'])) {

                    if ($user['thumb_url'] == '') {
                        $user['thumb_url'] = '/public/images/default.png';
                    }
                    session('level_id', $user['user_level_id']);
                    session('role_id', $user['user_role_id']);
                    session('thumb_url', $user['thumb_url']);
                    session('username', $user['username']);
                    session('nickname',$user['nickname']);
                    session('userId', $user['id']);
                    session('status', $user['status']);
                    session('user', $user);

                    $cook = array('id' => $user['id'], 'status' => $user['status'], 'username' => $user['username'], 'thumb_url' => $user['thumb_url'],'role'=>$user['user_role_id'], 'level' => $user['user_level_id']);

                    cookie('user',encryptMD5(implode('|',$cook)));


                    $member->update(
                        [
                            'last_login_time' => time(),
                            'last_login_ip' => $this->request->ip(),
                            'id' => $user['id'],
                        ]
                    );
                    //$this->add_log($user['id'], $user['username'], $user['nickname'].'登录于'.date('Y-m-d H:i:s',time()));

                    return json(array('code' => 200, 'msg' => '登录成功','url'=>url('/user/index')));
                } else {
                    return json(array('code' => 0, 'msg' => '密码错误'));
                }
            } else {
                return json(array('code' => 0, 'msg' => '账户不存在或已被禁用'));
            }
        }
        return json(array('code' => 0, 'msg' => '非法提交！'));
    }



}